﻿<div class="layui-fluid layui-anim febs-anim" id="febs-businessModule" lay-title="业务模块">
    <div class="layui-row layui-col-space8 febs-container">
        <div class="layui-col-md7 layui-col-sm6 layui-col-xs12">
            <div class="layui-card">
                <div class="layui-card-body febs-table-full">
                    <form class="layui-form layui-table-form" lay-filter="businessModule-table-form" id="businessModule-table-form">
                        <div class="layui-row">
                            <div class="layui-col-md9 layui-col-sm9 layui-col-xs9">
                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">模块名称</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="moduleName" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-col-md3 layui-col-sm12 layui-col-xs12 table-action-area">
                                <div class="layui-btn layui-btn-sm layui-btn-primary febs-button-blue-plain  table-action" id="query">
                                    <i class="layui-icon">&#xe848;</i>
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary febs-button-green-plain  table-action" id="reset">
                                    <i class="layui-icon">&#xe79b;</i>
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary table-action action-more">
                                    <i class="layui-icon">&#xe875;</i>
                                </div>
                            </div>
                        </div>
                    </form>
                    <table lay-filter="businessModuleTable" lay-data="{id: 'businessModuleTable'}"></table>
                </div>
            </div>
        </div>
        <div class="layui-col-md5 layui-col-sm6 layui-col-xs12">
            <div class="layui-card">
                <div class="layui-card-header" id="form-header">新增模块</div>
                <div class="layui-card-body febs-table-full">
                    <form class="layui-form layui-table-form" action="" lay-filter="businessModule-form">
                        <div class="layui-form-item febs-hide">
                            <label class="layui-form-label febs-form-item-require">模块id</label>
                            <div class="layui-input-block">
                                <input type="text" name="id" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label febs-form-item-require">模块名称</label>
                            <div class="layui-input-block">
                                <input type="text" name="moduleName" autocomplete="off" class="layui-input" minlength="1"
                                       lay-verify="range">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">模块描述：</label>
                            <div class="layui-input-block">
                                <textarea name="moduleDesc" class="layui-textarea"></textarea>
                            </div>
                        </div>
                        <div class="layui-form-item febs-hide">
                            <button type="reset" class="layui-btn" id="reset-form"></button>
                            <button class="layui-btn" lay-submit="" lay-filter="businessModule-form-submit"
                                    id="submit-form"></button>
                        </div>
                    </form>
                </div>
                <div class="layui-card-footer">
                    <button class="layui-btn" id="submit">保存</button>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="businessModule-option">
    <a lay-event="edit"><i class="layui-icon febs-edit-area febs-blue">&#xe7a4;</i></a>
    <a lay-event="del"><i class="layui-icon febs-edit-area febs-red">&#xe7f9;</i></a>
</script>
<script data-th-inline="none" type="text/javascript">
    layui.use(['dropdownMenu', 'jquery', 'laydate', 'febs', 'form', 'eleTree', 'table', 'validate'], function () {
        let $ = layui.jquery,
            laydate = layui.laydate,
            febs = layui.febs,
            form = layui.form,
            table = layui.table,
            dropdownMenu = layui.dropdownMenu,
            validate = layui.validate,
            $view = $('#febs-businessModule'),
            $query = $view.find('#query'),
            $reset = $view.find('#reset'),
            $submit = $view.find('#submit'),
            $searchForm = $view.find('#businessModule-table-form'),
            $header = $view.find('#form-header'),
            tableIns;

        form.verify(validate);
        form.render();

        initTable();

        laydate.render({
            elem: '#createTime',
            range: true
        });

        dropdownMenu.render({
            elem: $view.find('.action-more'),
            click: function (name, elem, event) {
                let checkStatus = table.checkStatus('businessModuleTable');
                if (name === 'add') {
                    resetbusinessModuleForm();
                    febs.alert.info("请在表单中填写相关信息");
                }
                if (name === 'delete') {
                    if (!checkStatus.data.length) {
                        febs.alert.warn('请选择需要删除的模块');
                    } else {
                        febs.modal.confirm('删除模块', '确定删除所选模块？', function () {
                            let businessModuleIds = [];
                            layui.each(checkStatus.data, function (key, item) {
                                businessModuleIds.push(item.id);
                            });
                            deletebusinessModules(businessModuleIds.join(','));
                        });
                    }
                }
            },
            options: [{
                name: 'add',
                title: '新增模块'
            }, {
                name: 'delete',
                title: '删除模块'
            }]
        });

        table.on('tool(businessModuleTable)', function (obj) {
            let data = obj.data,
                layEvent = obj.event;
            if (layEvent === 'edit') {
                $header.text('修改模块');
                form.val("businessModule-form", {
                    "id": data.id,
                    "moduleName": data.moduleName,
                    "moduleDesc": data.moduleDesc
                });
            }
            if (layEvent === 'del') {
                febs.modal.confirm('删除模块', '确定删除该模块？', function () {
                    deletebusinessModules(data.id);
                });
            }
        });

        $query.on('click', function () {
            resetbusinessModuleForm();
            tableIns.reload({where: getQueryParams(), page: {curr: 1}});
        });

        $reset.on('click', function () {
            resetbusinessModuleForm();
            $searchForm[0].reset();
            tableIns.reload({where: getQueryParams(), page: {curr: 1}});
        });

        $submit.on('click', function () {
            let data = form.val('businessModule-form')
            if (data.moduleName === '') {
                febs.alert.error('请填写模块名称')
            } else {
                addOrUpdatebusinessModule(data)
            }
            // addOrUpdatebusinessModule(data)
            // $view.find('#submit-form').trigger('click');
        });

        function initTable() {
            tableIns = febs.table.init({
                elem: $view.find('table'),
                id: 'businessModuleTable',
                url: ctx + 'businessModule/list',
                cols: [[
                    {type: 'checkbox'},
                    {title: '操作', toolbar: '#businessModule-option', width: 100},
                    {field: 'moduleName', title: '模块名称', minWidth: 120},
                    {field: 'moduleDesc', title: '模块描述'},
                    {field: 'createTime', title: '创建时间', minWidth: 180}
                ]]
            });
        }

        function getQueryParams() {
            let params = $searchForm.serializeJson();
            params.invalidate_ie_cache = new Date();
            return params;
        }

        function resetbusinessModuleForm() {
            $view.find('#reset-form').trigger('click');
            $header.text('新增模块');
        }

        form.on('submit(businessModule-form-submit)', function (data) {
            // addOrUpdatebusinessModule(data.field);
        });

        function deletebusinessModules(businessModuleIds) {
            febs.get(ctx + 'businessModule/remove/' + businessModuleIds, null, function (result) {
                if (result.code === 200) {
                    febs.alert.success('删除模块成功');
                    $query.trigger('click');
                } else {
                    febs.alert.error(result.message)
                }
            })
        }

        let addOrUpdatebusinessModule = function (data) {
            if (data.id && $header.text() === '修改模块') {
                febs.post(ctx + 'businessModule/update', data, function (result) {
                    if (result.code === 200) {
                        febs.alert.success('修改模块成功');
                        $query.trigger('click');
                    } else {
                        febs.alert.error(result.message)
                    }
                });
            } else {
                febs.post(ctx + 'businessModule/add', data, function (result) {
                    if (result.code === 200) {
                        febs.alert.success('新增模块成功');
                        $query.trigger('click');
                    } else {
                        febs.alert.error(result.message)
                    }
                });
            }
        }
    });
</script>